package com.one.controller;

import com.one.service.NoticeService;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description 公告管理
 * @Author semstouch
 * @Date 2017/4/7
 **/
@Controller
@ControllerAdvice
public class NoticeController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private NoticeService noticeService;

    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;


    public NoticeController() {
        super("callback");
    }

    @RequestMapping(value = "/getNoticeList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getNoticeList(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        jsonResult.setData(noticeService.findAll());
        jsonResult.setCode("1");
        return  jsonResult;
    }

}
